package com.example.directory;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public Button add,query,update,delete;
    public TextView etName,etNumber,tvShow;
    public String name,number;
    public MyHelper myhelper;
    SQLiteDatabase db;//获取可读数据库对象
    ContentValues values;
    public List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myhelper = new MyHelper(this);

        init();

    }
//初始化控件
    private void init() {
        add = findViewById(R.id.btn_add);
        query = findViewById(R.id.btn_query);
        update = findViewById(R.id.btn_update);
        delete = findViewById(R.id.btn_delete);
        tvShow = findViewById(R.id.tv_show);
        etName = findViewById(R.id.et_name);
        etNumber = findViewById(R.id.et_number);

        add.setOnClickListener(this);
        query.setOnClickListener(this);
        update.setOnClickListener(this);
        delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
//            点击添加按钮
            case R.id.btn_add:
                name = etName.getText().toString();
                number = etNumber.getText().toString();
                values = new ContentValues();
                values.put("name",name);
                values.put("phone",number);
                db = myhelper.getReadableDatabase();
                db.insert("information",null,values);
//                db.close();
                break;
//                点击查询的按钮
            case R.id.btn_query:
                db = myhelper.getReadableDatabase();
                Cursor cursor = db.query("information",null,null,null,null,null,null);
                if(cursor.getCount() == 0){
                    tvShow.setText("");
                    Toast.makeText(this,"您的通讯录还没有联系人哦",Toast.LENGTH_LONG).show();;
                }else{
                    cursor.moveToFirst();
                    //tvShow.setText("联系人："+cursor.getString(1)+"    电话："+cursor.getString(2));
                        Map<String, Object> show = new HashMap<String, Object>();
                        show.put("name", cursor.getString(1));
                        show.put("number", cursor.getString(2));
                        list.add(show);

                    //创建一个simpleAdapter
                    SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(),
                            list,//要显示的数据
                            R.layout.list_item, //指定Item的布局
                            new String[]{"name", "number"},//listitem中数据的key
                            new int[]{R.id.name, R.id.number});//Item布局的组件id
                    ListView listView = findViewById(R.id.my_list);
                    listView.setAdapter(myAdapter);
                }
                while (cursor.moveToNext()){
                        Map<String, Object> show = new HashMap<String, Object>();
                        show.put("name", cursor.getString(1));
                        show.put("number", cursor.getString(2));
                        list.add(show);
                    }

                    //创建一个simpleAdapter
                    SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(),
                            list,//要显示的数据
                            R.layout.list_item, //指定Item的布局
                            new String[]{"name", "number"},//listitem中数据的key
                            new int[]{R.id.name, R.id.number});//Item布局的组件id
                    ListView listView = findViewById(R.id.my_list);
                    listView.setAdapter(myAdapter);
                cursor.close();
//                db.close();
                break;
//                点击修改的按钮
            case R.id.btn_update:
                db=myhelper.getWritableDatabase();
                values = new ContentValues();
                values.put("phone",number = etNumber.getText().toString());
                db.update("information",values,"name=?",new String[]{etName.getText().toString()});
//                db.close();
                break;
//                点击删除的按钮
            case R.id.btn_delete:
                db=myhelper.getWritableDatabase();
                db.delete("information",null,null);
                Toast.makeText(this,"数据已删除",Toast.LENGTH_LONG).show();
                tvShow.setText("");
//                db.close();
                break;

        }

    }
}
